package model

import (
	"github.com/spf13/viper"
	"time"
	"github.com/BSNDA/PCNGateway-Go-SDK/pkg/core/entity/req/fabric/node"
	"log"
	"math/rand"
)

type QueryLocation struct {
	OrderId string `json:"orderid"`
}


//用户住址上链
func  SetLocation(message string ) (string, error) {
	//随机数nonce生成
	rand.Seed(time.Now().UnixNano())//放在最外层
	bytes:=RandomByte(24)
	nonce:=Base64Encrypt(bytes)

	//传入string
	var orderdata = []string {message}
	var  trans  node.TransReqDataBody
	trans.Args=orderdata
	trans.ChainCode=viper.GetString("bc.chaincode")
	trans.FuncName="set"     //链码功能名
	trans.Nonce=nonce
	client:=BCinit()
	res,err:=client.ReqChainCode(node.TransReqDataBody(trans))
	if err != nil {
		log.Fatal(err)
	}
	var messages BCMessage
	messages.Body=res.Body.CCRes.CCData
	return messages.Body,nil
}

//通过订单号查询用户住址
func  GetLocation(data string) (string, error) {
	rand.Seed(time.Now().UnixNano())//放在最外层
	bytes:=RandomByte(24)
	nonce:=Base64Encrypt(bytes)

	var  trans  node.TransReqDataBody
	var messages BCMessage
	var  test1 =[]string {data}
	trans.Args=test1   //通过key查values
	trans.ChainCode=viper.GetString("bc.chaincode")
	trans.FuncName="get"
	trans.Nonce=nonce
	//fbclient :=fabric.FabricClient{}
	client:=BCinit()
	res,err  := client.ReqChainCode(node.TransReqDataBody(trans))
	if err != nil {
		log.Fatal(err)
	}
	messages.Body=res.Body.CCRes.CCData
	return messages.Body,nil
}